clear 
clear matrix
set memory 1000m
set more off
cap log close

cd "/Users/..."

global do_file="‎⁨/Users/.../do_file"
global log_file="/Users/.../log_file"
global raw_data="/Users/.../raw_data⁩⁩"
global working_data="/Users/.../working_data"
global results="/Users/.../results/072020/placebo"




***********************************************************
***********************************************************
* Placebo Tests: switch the value for HB and risky behavior
***********************************************************
***********************************************************

use working_data/clean_day_level_geo_weather_081519.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index


** create duplicate values
foreach dep in "total_prev_hard_brake" "total_agg_acc" "total_phone_use" "distance" "duration" "speed" "drive_at_night" "highway"{
	gen `dep'_dup=`dep'
	drop `dep'
}


foreach dep in "total_phone_use" "distance" "duration" "speed" "drive_at_night" "highway"{
	
	gen total_prev_hard_brake=total_prev_hard_brake_dup
	gen total_phone_use=total_phone_use_dup
	gen distance=distance_dup
	gen duration=duration_dup
	gen speed=speed_dup
	gen drive_at_night=drive_at_night_dup
	gen highway=highway_dup
	

	display "`dep'"
	rename `dep' temp
	rename total_prev_hard_brake temp1
	
	rename temp total_prev_hard_brake
	rename temp1 `dep'
	
	sum total_prev_hard_brake
	
	
	************************
	* Gen fisrt-diff for NM
	************************


	*-- (1) orig hard brake
	gen diff_prev_hard_brake=total_prev_hard_brake-l.total_prev_hard_brake


	************************************
	* Gen fisrt-diff for risky behavior
	************************************


	gen diff_phone_use=total_phone_use-l.total_phone_use

	gen diff_traffic_jam=total_traffic_jam-l.total_traffic_jam
	gen diff_traffic_time=total_traffic_sec-l.total_traffic_sec
	gen diff_distance=distance-l.distance
	gen diff_duration=duration-l.duration
	gen diff_speed=speed-l.speed
	gen diff_speed2=speed2-l.speed2
	gen diff_drive_at_night=drive_at_night-l.drive_at_night
	gen diff_night_prop=night_duration_prop-l.night_duration_prop
	gen diff_night_duration=total_night_duration-l.total_night_duration
	gen diff_weekend=weekend-l.weekend
	gen diff_rush_hour=rush_hour-l.rush_hour

	gen diff_control_score=control_score-l.control_score
	gen diff_cautious_score=cautious_score-l.cautious_score
	gen diff_focused_score=focused_score-l.focused_score
	gen diff_drive_score=drive_score-l.drive_score
	gen diff_gap_time=gap_time-l.gap_time

	gen diff_highway=highway-l.highway

	** gen first_difference for weather variables, 
	gen diff_sunny=sunny-l.sunny
	gen diff_rain=rain-l.rain
	gen diff_snow=snow-l.snow
	gen diff_rain_storm=rain_storm-l.rain_storm
	gen diff_cloudy=cloudy-l.cloudy

	gen diff_high_temper=high_temper-l.high_temper
	gen diff_low_temper=low_temper-l.low_temper

	
	
	
	
	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.diff_prev_hard_brake l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_`dep'_072020.xls", append keep(l.diff_prev_hard_brake)
	
	
	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.diff_prev_hard_brake l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_`dep'_072020.xls", append keep(l.diff_prev_hard_brake)
	
	
	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.diff_prev_hard_brake l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_`dep'_072020.xls", append keep(l.diff_prev_hard_brake)
	
	
	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.diff_prev_hard_brake l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_`dep'_072020.xls", append keep(l.diff_prev_hard_brake)
	
	
	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.diff_prev_hard_brake l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_`dep'_072020.xls", append keep(l.diff_prev_hard_brake)
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.diff_prev_hard_brake l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_`dep'_072020.xls", append keep(l.diff_prev_hard_brake)
	
	drop `dep'-diff_low_temper
}





*************************************************
*************************************************
* Placebo Tests: use other time-varying variables
*************************************************
*************************************************

use working_data/clean_day_level_geo_weather_081519.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index


************************
* Gen fisrt-diff for NM
************************


*-- (1) hard brake
gen diff_hard_brake=total_hard_brake-l.total_hard_brake

************************************
* Gen fisrt-diff for risky behavior
************************************

gen diff_agg_acc=total_agg_acc-l.total_agg_acc
gen diff_phone_use=total_phone_use-l.total_phone_use

gen diff_traffic_jam=total_traffic_jam-l.total_traffic_jam
gen diff_traffic_time=total_traffic_sec-l.total_traffic_sec
gen diff_distance=distance-l.distance
gen diff_duration=duration-l.duration
gen diff_speed=speed-l.speed
gen diff_speed2=speed2-l.speed2
gen diff_drive_at_night=drive_at_night-l.drive_at_night
gen diff_night_prop=night_duration_prop-l.night_duration_prop
gen diff_night_duration=total_night_duration-l.total_night_duration
gen diff_weekend=weekend-l.weekend
gen diff_rush_hour=rush_hour-l.rush_hour

gen diff_control_score=control_score-l.control_score
gen diff_cautious_score=cautious_score-l.cautious_score
gen diff_focused_score=focused_score-l.focused_score
gen diff_drive_score=drive_score-l.drive_score
gen diff_gap_time=gap_time-l.gap_time

gen diff_highway=highway-l.highway

** gen first_difference for weather variables, 
gen diff_sunny=sunny-l.sunny
gen diff_rain=rain-l.rain
gen diff_snow=snow-l.snow
gen diff_rain_storm=rain_storm-l.rain_storm
gen diff_cloudy=cloudy-l.cloudy

gen diff_high_temper=high_temper-l.high_temper
gen diff_low_temper=low_temper-l.low_temper



** driving score

	
	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_drive_score_072020.xls", replace keep(l.diff_drive_score)
	
	
	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_drive_score_072020.xls", append keep(l.diff_drive_score)
	
	
	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_drive_score_072020.xls", append keep(l.diff_drive_score)
	
	
	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_drive_score_072020.xls", append keep(l.diff_drive_score)
	
	
	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_drive_score_072020.xls", append keep(l.diff_drive_score)
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_drive_score_072020.xls", append keep(l.diff_drive_score)

	
** weather: rain


	
	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_snow ///
	(l.diff_phone_use l.diff_rain l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_rain_072020.xls", replace keep(l.diff_rain)
	
	
	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny   diff_snow ///
	(l.diff_distance l.diff_rain l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_rain_072020.xls", append keep(l.diff_rain)
	
	
	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny   diff_snow ///
	(l.diff_duration l.diff_rain l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_rain_072020.xls", append keep(l.diff_rain)
	
	
	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny   diff_snow ///
	(l.diff_speed l. diff_rain l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_rain_072020.xls", append keep(l.diff_rain)
	
	
	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny   diff_snow ///
	(l.diff_drive_at_night l.diff_rain l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_rain_072020.xls", append keep(l.diff_rain)
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny   diff_snow ///
	(l.diff_highway l.diff_rain l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_rain_072020.xls", append keep(l.diff_rain)

** weather: high temper


	
	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	 diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.diff_high_temper l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_high_temper_072020.xls", replace keep(l.diff_high_temper)
	
	
	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	 diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.diff_high_temper l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_high_temper_072020.xls", append keep(l.diff_high_temper)
	
	
	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	 diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.diff_high_temper l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_high_temper_072020.xls", append keep(l.diff_high_temper)
	
	
	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	 diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.diff_high_temper l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_high_temper_072020.xls", append keep(l.diff_high_temper)
	
	
	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	 diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.diff_high_temper l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_high_temper_072020.xls", append keep(l.diff_high_temper)
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	 diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.diff_high_temper l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_high_temper_072020.xls", append keep(l.diff_high_temper)

 	
** traffic	
	
	
	ivregress gmm diff_phone_use  diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.diff_traffic_jam l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_traffic_072020.xls", replace keep(l.diff_traffic_jam)
	
	
	ivregress gmm diff_distance  diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.diff_traffic_jam l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_traffic_072020.xls", append keep(l.diff_traffic_jam)
	
	
	ivregress gmm diff_duration  diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.diff_traffic_jam l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_traffic_072020.xls", append keep(l.diff_traffic_jam)
	
	
	ivregress gmm diff_speed  diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.diff_traffic_jam l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_traffic_072020.xls", append keep(l.diff_traffic_jam)
	
	
	ivregress gmm diff_drive_at_night  diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.diff_traffic_jam l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_traffic_072020.xls", append keep(l.diff_traffic_jam)
	
	
	ivregress gmm diff_highway  diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.diff_traffic_jam l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using "$results/placebo_traffic_072020.xls", append keep(l.diff_traffic_jam)
